#!/usr/bin/perl -w
use strict;
use DBI;
#Copyright 2011. Jason Weirather.
#This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3 or any later version.
#take as an input a FASTA file and convert it to multiple FASTA files with my tab labels.
#this is working.  It requires mcloutput_to_labeled_fasta.pl to work

my $store_work_dir = "/home/x/Public/visblast/work";
if(scalar(@ARGV)<3) { die "./mcl_fasta_to_labeled_fastas.pl input.fasta output.fasta evalue ivalue\nevalue is for inital blast mapping\nivalue is for mcl cluster (smaller takes longer)"; }
my $infile = shift @ARGV;
my $outfile = shift @ARGV;
my $evalue = shift @ARGV;
my $ivalue = shift @ARGV;
my $baseoutname = "temp";
if($outfile =~ /(\w+)$/) {
  $baseoutname = $1;
}
my $eval = $evalue;
my $blast_score_option = 'r';
my $iran = rand();
$iran =~ s/0\.//; #generate a random value for a filename
$iran =~ s/\.//; # rare case of 1 ?
`./interBlast.pl -i $infile -o mfir$iran -e $eval`;
`mclblastline --xo-dat=msec$iran --blast-m9 --blast-score=$blast_score_option --blast-sort=a --mcl-I=$ivalue mfir$iran`;
### find the filename of this guy to pass.
my $moutname = "out.mfir$iran.msec$iran";
my @cannames = `ls $moutname.*`;
chomp( my $result_name = shift @cannames);
$moutname = $result_name;
if(!$moutname) { die; }
my $indexname = "mfir$iran.msec$iran.tab";
`./mcloutput_to_labeled_fasta.pl $moutname $indexname $outfile $infile`;
`./fasta_clusters_to_hmm.pl $outfile`;
`mv $outfile-hmm.zip $store_work_dir/$baseoutname-hmm.zip`;
`mv $outfile-fasta.zip $store_work_dir/$baseoutname-fasta.zip`;
`mv $outfile-selex.zip $store_work_dir/$baseoutname-selex.zip`;
unlink($moutname);
my $namebase = "mfir$iran.msec$iran";
unlink("mfir$iran"); # this is the blast result file (comments still in it)
unlink("$namebase.err");
unlink("$namebase.hdr");
unlink("$namebase.map");
unlink("$namebase.raw");
unlink("$namebase.sym");
unlink("$namebase.tab");
unlink("dump.$moutname");
